﻿Imports System.IO

<Serializable()> _
Public Class clsNotificationImage
	Inherits clsBaseClass

	Private Shared dicSystemImages As Dictionary(Of String, Image)
	Private Shared WithEvents frm As frmScreen

	Private booShowImageFile As Boolean
	Private booShowSystemImage As Boolean

	Private strImageFilePath As String
	Private strSystemImage As String

	Private imgImage As Image

	Public Event ImageLoadCompleted(ByVal sender As Object, ByVal e As Image)

	Public Property Image() As Image
		Get
			Return imgImage
		End Get
		Set(ByVal value As Image)
			imgImage = value
		End Set
	End Property

	Public Property ImageFilePath() As String
		Get
			Return strImageFilePath
		End Get
		Set(ByVal value As String)
			strImageFilePath = value
		End Set
	End Property

	Public Property ShowSystemImage() As Boolean
		Get
			Return booShowSystemImage
		End Get
		Set(ByVal value As Boolean)
			booShowSystemImage = value
		End Set
	End Property

	Public Property ShowImageFile() As Boolean
		Get
			Return booShowImageFile
		End Get
		Set(ByVal value As Boolean)
			booShowImageFile = value
		End Set
	End Property

	Public Property SystemImage() As String
		Get
			Return strSystemImage
		End Get
		Set(ByVal value As String)
			strSystemImage = value
		End Set
	End Property

	Public Shared ReadOnly Property SystemImages() As Dictionary(Of String, Image)
		Get
			Return dicSystemImages
		End Get
	End Property

	Public Function ShowImage(ByVal booAutoHide As Boolean)
		Dim result = LoadImage(frm.picScreen)
		If frm.Visible = False Then
			frm.Visible = True
			frm.tmrHide.Enabled = booAutoHide
		End If
		Return result
	End Function

	Public Sub OnImageLoadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
		If sender Is Nothing Then Exit Sub
		Dim pic As PictureBox = sender
		If pic Is Nothing Then Exit Sub
		Image = pic.Image
		RaiseEvent ImageLoadCompleted(sender, pic.Image)
	End Sub

	Private Sub SetSystemImage(ByVal picBox As PictureBox)
		If picBox Is Nothing Or SystemImage Is Nothing Then Exit Sub
		If dicSystemImages.ContainsKey(SystemImage) = False Then Exit Sub
		Dim img = dicSystemImages(SystemImage)
		picBox.Image = img
	End Sub

	Public Function LoadImage(ByVal picBox As PictureBox) As Boolean
		If picBox Is Nothing Then Return False
		Try
			If ShowImageFile = False Then
				If picBox.InvokeRequired Then
					Dim m As New Action(Of PictureBox)(AddressOf SetSystemImage)
					picBox.Invoke(m, picBox)
				Else
					SetSystemImage(picBox)
				End If
			Else
				If Not File.Exists(ImageFilePath) Then
					picBox.Image = Nothing
					Return False
				End If
				If picBox.InvokeRequired Then
					Dim m As New Action(Of String)(AddressOf picBox.LoadAsync)
					picBox.Invoke(m, ImageFilePath)
				Else
					picBox.LoadAsync(ImageFilePath)
				End If
			End If
			Return True
		Catch ex As Exception
			modCommon.RegisterException(ex)
		End Try
		Return False
	End Function

	Public Overrides Sub Dispose()
		ImageFilePath = Nothing
		MyBase.Dispose()
	End Sub

	Public Overrides Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
		MyBase.GetObjectData(info, context)
		info.AddValue("ShowImageFile", ShowImageFile)
		info.AddValue("ShowSystemImage", ShowSystemImage)
		info.AddValue("SystemImage", SystemImage)
		info.AddValue("ImageFilePath", ImageFilePath)
	End Sub

	Public Sub New()
		If frm Is Nothing Then
			frm = New frmScreen
			frm.Text = My.Application.Info.ProductName
			AddHandler frm.picScreen.LoadCompleted, AddressOf OnImageLoadCompleted
		End If

		If SystemImages IsNot Nothing Then Exit Sub
		InitSystemImages()
	End Sub

	Public Shared Sub InitSystemImages()
		If SystemImages Is Nothing Then
			dicSystemImages = New Dictionary(Of String, Image)
		Else
			SystemImages.Clear()
		End If

		SystemImages.Add("ALT Key", My.Resources.ScreenImages.img_scr_alt_key)
		SystemImages.Add("CTRL Key", My.Resources.ScreenImages.img_scr_ctrl_key)
		SystemImages.Add("SHIFT Key", My.Resources.ScreenImages.img_scr_shift_key)
		SystemImages.Add("Caps Lock", My.Resources.ScreenImages.img_scr_caps_lock)
		SystemImages.Add("Num Lock", My.Resources.ScreenImages.img_scr_num_lock)
		SystemImages.Add("Scroll Lock", My.Resources.ScreenImages.img_scr_scroll_lock)
		SystemImages.Add("Language Icon", My.Resources.ScreenImages.img_scr_language)
		SystemImages.Add("Alert Icon", My.Resources.ScreenImages.img_scr_alert)
		SystemImages.Add("Keyboard Icon", My.Resources.ScreenImages.img_scr_keyboard)
		SystemImages.Add("Lock Icon", My.Resources.ScreenImages.img_scr_lock)
	End Sub

	Public Shared Function GetSystemImagesList() As String()
		If SystemImages Is Nothing Then InitSystemImages()
		If SystemImages Is Nothing Then Return Nothing

		Dim ary(SystemImages.Count - 1) As String
		SystemImages.Keys.CopyTo(ary, 0)
		Return ary
	End Function
End Class
